<%--
  Created by IntelliJ IDEA.
  User: lujw
  Date: 2017/6/14
  Time: 16:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/validate.jsp"%>
<%!
    /**
     * 院内用户登录
     * @param hospitalUser
     * @return
     */
    public static Message login(HospitalUser hospitalUser) {
        Message msg = new Message();
        msg.put("data", Collections.EMPTY_MAP);
        if (Objects.isNull(hospitalUser)) {
            ErrcodeUtil.setErrcode(msg, ErrcodeUtil.LOGIN_FAILED);
            return msg;
        }
        String phone = Tools.getDesc(hospitalUser.getPhone(), "");
        String password = Tools.getDesc(hospitalUser.getPassword(), "");
        if (ValidateUtil.isEmpty(phone)) {
            msg.setErrcodeAndErrmsg(30999, "请输入手机号");
            return msg;
        }
        if (!ValidateUtil.validatePhone(phone)) {
            msg.setErrcodeAndErrmsg(30999, "请输入正确的手机号");
            return msg;
        }
        List<HospitalUser> hospitalUsers = QBuilder.db("basedb").select().from("HospitalUser").where(new Where("phone=", phone).and("password=", password).and("status=", ConstantUtil.STATUS_ENABLE)).page(1,1).query().getResultObj();
        if (hospitalUsers.size() == 0) { // 登录失败
            msg.setErrcodeAndErrmsg(ErrcodeUtil.LOGIN_FAILED, "账号或密码错误");
            return msg;
        }

        //设置最后登录时间和ip
        HospitalUser hospital_user = hospitalUsers.get(0);
        hospital_user.setLastLogin(System.currentTimeMillis());
        hospital_user.setLastIp(hospitalUser.getLastIp());
        DBTools.db("basedb").modItem(hospital_user, hospital_user.getId());

        Map<String, Object> userMap = hospitalUserInfo(hospital_user);
        Map tokenMap = token(hospital_user);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("token", tokenMap.get("token"));
        map.put("expires_in", tokenMap.get("expireIn"));
        Map dataMap = new HashMap();
        dataMap.put("auth", map);
        dataMap.put("user", userMap);
        msg.put("data", dataMap);
        msg.setErrmsg("登录成功");
        return msg;
    }

    /**
     * 设置用户详细信息
     * @param hospitalUser
     * @return
     */
    public static Map<String, Object> hospitalUserInfo(HospitalUser hospitalUser) {
        if (Objects.isNull(hospitalUser)) {
            return Collections.emptyMap();
        }
        Map<String, Object> hospitalUserMap = new HashMap<String, Object>();
        hospitalUserMap.put("user_id", hospitalUser.getId());
        hospitalUserMap.put("name", hospitalUser.getName());
        hospitalUserMap.put("employee_id", hospitalUser.getEmployeeId());
        hospitalUserMap.put("phone", hospitalUser.getPhone());
        hospitalUserMap.put("card_number", hospitalUser.getCardNumber());
        hospitalUserMap.put("wechat", hospitalUser.getWechat());
        hospitalUserMap.put("qq", hospitalUser.getQq());
        hospitalUserMap.put("email", hospitalUser.getEmail());
        hospitalUserMap.put("avatar", hospitalUser.getAvatar());
        hospitalUserMap.put("gender", hospitalUser.getGender());
        hospitalUserMap.put("entry_time", hospitalUser.getEntryTime());
        hospitalUserMap.put("personal_secret", hospitalUser.getPersonalSecret());
        hospitalUserMap.put("hospital", hospitalUser.getHospital());
        hospitalUserMap.putAll(hospitalUser.getJson());

        //查询院内用户所在科室
        hospitalUserMap.put("dept", UserUtil.getUserDept(hospitalUser.getHospital(), hospitalUser.getId()));

        //查询院内用户所在标签
        hospitalUserMap.put("label", UserUtil.getUserCustomizeLabel(hospitalUser.getHospital(), hospitalUser.getId()));
        return hospitalUserMap;
    }

    /**
     * 生成令牌(院内用户)
     * @param hospitalUser
     * @return
     */
    public static Map token(HospitalUser hospitalUser) {
        if (Objects.isNull(hospitalUser))
            return Collections.emptyMap();
        HospitalToken token = new HospitalToken();
        token.setUid(hospitalUser.getId());
        token.setUserName(hospitalUser.getPhone());
        token.setPassword(hospitalUser.getPassword());
        token.setStatus(ConstantUtil.STATUS_ENABLE);
        token.setHospital(hospitalUser.getHospital());
        return token.genToken();
    }
%>
<%
    parameters.put("phone", parameters.get("user_name"));
    HospitalUser hospitalUser = HospitalUser.fromMap(parameters, HospitalUser.class);
    hospitalUser.setLastIp(parameters.get("ip"));
    Tools.encryptPrint(out, login(hospitalUser));
%>
